package com.zyx.mpdemo.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.zyx.mpdemo.helpers.injector.EasySqlInjector;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Yaxi.Zhang
 * @since 2022/3/29 10:56
 */
@Configuration
// 扫描mapper接口所在的包，可以从启动类移动到该配置类
@MapperScan("com.zyx.mpdemo.mapper")
public class MyBatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 添加分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // 添加乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

    /**
     * 定义核心配置类注入EasySqlInjector
     * 让mybatis-plus可以使用InsertBatchSomeColumn方法进行批量插入
     */
    @Bean
    public EasySqlInjector sqlInjector() {
        return new EasySqlInjector();
    }

}
